package com.xiaxiang.xiawan.My;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pbq.imagepicker.VideoDataSource;
import com.pbq.imagepicker.bean.VideoFolder;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;
import com.xiaxiang.ar.UnityActivity;
import com.xiaxiang.xiawan.My.Adapter.LocalPhotoAdapter;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.Util.XXToolUtil;
import com.xiaxiang.xiaxwan.ar.data.ConfigDataTool;
import com.xiaxiang.xiaxwan.ar.data.XXARMaterialVideoData;

import java.util.List;

import static com.pbq.imagepicker.ui.video.VideoGridActivity.REQUEST_PERMISSION_STORAGE;

public class LocalPhotoActivity extends AppCompatActivity implements VideoDataSource.OnVideosLoadedListener {
    String title = "本地相册";
    String url = null;
    GridView photo_list;
    private LocalPhotoAdapter adapter;
    private TextView TitleBarRigthText;
    private Button btnDelete;
    private RxDialogSureCancel rxDialogSureCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        url = UnityActivity.getDefaultVideoPath( this.getPackageName());
        setContentView(R.layout.activity_local_photo);
        photo_list = (GridView) findViewById(R.id.photo_list);
        adapter = new LocalPhotoAdapter(this);
        photo_list.setAdapter(adapter);
        initTitle();

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            if (checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                //加载手机中的视频数据
                new VideoDataSource(this, url, this);
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
            }
        }
        btnDelete = (Button) findViewById(R.id.btnDelete);
        setListener();
    }

    private void setListener() {
        TitleBarRigthText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.clearSelectedVideos();
                if (adapter.isEdit()){
                    TitleBarRigthText.setText("编辑");
                    adapter.setEdit(false);
                    btnDelete.setVisibility(View.GONE);
                }else {
                    TitleBarRigthText.setText("取消");
                    adapter.setEdit(true);
                    btnDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        photo_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (adapter.isEdit()){

                }
                else{
                    XXARMaterialVideoData mMaterialData;
                    mMaterialData = new XXARMaterialVideoData();
                    Intent intent = new Intent("com.xiaxiang.xiawan.video.preview");
                    intent.setPackage(getPackageName());
                    if(mMaterialData != null){
                        mMaterialData.mVideoPath = adapter.getItem(i).path;
                        intent.putExtra(ConfigDataTool.MATERIAL_VIDEO_DATA_KEY,mMaterialData);
                    }else{
                        mMaterialData = new XXARMaterialVideoData();
                    }
                    startActivity(intent);
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = adapter.getmSelectedVideoNum();
                if (num > 0) {
                    rxDialogSureCancel = new RxDialogSureCancel(getActivity(), R.layout.dialog_makesure);//提示弹窗
                    rxDialogSureCancel.setContent("确认要删除这"+ String.valueOf(num) +"个视频吗？");
                    rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            adapter.deleteSelectedVideos();
                            rxDialogSureCancel.cancel();
                        }
                    });
                    rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            rxDialogSureCancel.cancel();
                        }
                    });
                    rxDialogSureCancel.show();
                }else {
                    showToastTips("未选中图片");
                }
//                adapter.deleteSelectedVideos();
            }
        });
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                new VideoDataSource(this, url, this);
            } else {
//                showToast("权限被禁止，无法选择本地视频");
            }
        }
    }

    private boolean checkPermission(String permission) {
        return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }

    private void initTitle() {
        TitleBarRigthText = (TextView) findViewById(R.id.TitleBarRigthText);
        TitleBarRigthText.setText("编辑");
        TitleBarRigthText.setVisibility(View.VISIBLE);
        ((TextView)findViewById(R.id.TitleBarText)).setText(title);
        ImageView back = (ImageView) findViewById(R.id.TitleBarLeftImg);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onVideosLoaded(List<VideoFolder> lders) {
        if (lders != null && lders.size() > 0) {
            Log.i("drj", "1" + lders.toString());
            adapter.setData(lders.get(0).videos);
        }
    }

    protected void showToastTips(final String tips) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast mToast = Toast.makeText(getActivity(), tips, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    public Activity getActivity() {
        return this;
    }
}
